package com.hqyj.ctxp.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hqyj.ctxp.mapper.ShopCartMapper;
import com.hqyj.ctxp.pojo.ShopCart;
import com.hqyj.ctxp.service.ShopCartService;
import com.hqyj.ctxp.utils.ResultInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/shopCart")
public class ShopCartController {

    @Autowired
    private ShopCartService shopCartService;

    @Autowired
    private ShopCartMapper shopCartMapper;

    // 添加到购物车
    @PutMapping("/addCart")
    public ResultInfo addCart(@RequestBody ShopCart shopCart) {
        //更新购物车相同的商品数量
        //先查询有没有相同的商品
        Integer productId = shopCart.getProductId();
        Integer userNo = shopCart.getUserNo();
        QueryWrapper<ShopCart> wrapper = new QueryWrapper<ShopCart>().eq("user_no", userNo)
                .eq("product_id", productId);
        ShopCart shopCart1 = shopCartMapper.selectOne(wrapper);
        if(shopCart1==null) {
            return shopCartService.addCart(shopCart);
        }
        return shopCartService.updateNum(shopCart1);
    }

    //查看当前用户购物车
    @PostMapping("/showUserCart/{userNo}")
    public ResultInfo showUserCart(@PathVariable Integer userNo){
        return shopCartService.showUserCart(userNo);
    }

    //删除购物车商品
    @DeleteMapping("/deleteProduct")
    public ResultInfo deleteProduct(@RequestBody ShopCart shopCart){
        return shopCartService.deleteProduct(shopCart);
    }
}
